<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <span>{{ list.length }}</span>
      <!--切换推荐和最新-->
      <a
        @click="handle(null)"
        :class="[obj.sorter == null ? 'active' : '']"
        href="javascript:;"
        >推荐</a
      >
      <a
        @click="handle('weight_desc')"
        :class="[obj.sorter == 'weight_desc' ? 'active' : '']"
        href="javascript:;"
        >最新</a
      >
      <div class="logo">
        <img alt src="@/assets/logo.png" />
      </div>
    </nav>

    <van-pull-refresh @refresh="onRefresh" v-model="refreshing">
      <van-list
        :finished="finished"
        @load="onLoad"
        finished-text="没有更多了"
        offset="80"
        style="padding-bottom: 30px"
        v-model="loading"
      >
        <!--
          1.组件本身是没有点击事件的,如果写了事件,那么只能是子组件传过来的
          2.如果组件想要写点击事件,必须要加上修饰符native 
        -->
        <!-- <ArticleItem
          :item="item"
          :key="item.id"
          v-for="item in list"
          @click="godetail(item.id)"
        ></ArticleItem> -->

        <ArticleItem
          :item="item"
          :key="item.id"
          v-for="item in list"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>

import axios from 'axios'
import { ArtilceAPI } from "@/api/artilce.js"
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      obj: {
        current: 1,
        pageSize: 10,
        sorter: null
      }
    }
  },
  created () {

  },
  methods: {
    //添加面经
    // async add () {
    //   const res1 = await axios({
    //     url: 'http://interview-api-t.itheima.net/admin/interview/create',
    //     headers: {
    //       Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjIxMiIsImlhdCI6MTY4NTYwNDcwMSwiZXhwIjoxNjg1ODYzOTAxfQ.myKSqwYyWLHbJAM2bzIiSBO3b2TGlpP_O9czn2yxhLs'
    //     },
    //     method: 'post',
    //     data: {
    //       stem: '132',
    //       content: '4545454545'
    //     }

    //   })
    //   console.log(res1, 555);
    // },

    godetail (id) {
      console.log(id, 2222);
    },




    //点击谁,就把谁的值传入obj.sorter,同一个方法但是参数不同,类似重载
    handle (sorter) {
      this.obj.sorter = sorter
      this.refreshing = true
      this.onRefresh()
    },
    //上拉加载
    onLoad () {
      if (this.obj.current > 3) {
        this.finished = true
        return
      }
      this.obj.current++
      this.getArtilceAPI()
    },
    //下拉刷新
    onRefresh () {
      // 清空列表数据
      this.finished = false;
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      //设置页码为第一页,显示第一页的数据
      this.obj.current = 0
      this.onLoad();
    },
    async getArtilceAPI () {
      try {
        const res = await ArtilceAPI(this.obj)
        // console.log(res);
        // this.list = res.data.rows
        this.list = this.refreshing ? res.data.rows : this.list.concat(...res.data.rows)
        this.loading = false//结束上拉加载 
        this.refreshing = false//结束
        // console.log(this.list);



      } catch (error) {

      }


    }

  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>